package com.qire.other.thirdPlatform;

import com.qire.other.thirdPlatform.authorize.ThirdPlatformAuthorization;
import com.qire.other.thirdPlatform.authorize.ThirdPlatformAuthorizeCallBack;
import com.qire.other.thirdPlatform.authorize.ThirdPlatformUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * 腾讯QQ平台认证登录
 */
public class QqPlatform extends ThirdPlatform implements ThirdPlatformAuthorization {

    public static QqPlatform build() {
        return new QqPlatform();
    }

    private ThirdPlatformAuthorizeCallBack callBack;

    PlatformActionListener platformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int code, HashMap<String, Object> hashMap) {
            if(callBack == null) {
                return;
            }
            if (platform.getName().equals(QQ.NAME)) {
                ThirdPlatformUserInfo userInfo = new ThirdPlatformUserInfo();
                userInfo.openid = platform.getDb().getUserId();
                userInfo.nickname = hashMap.get("nickname").toString();
                userInfo.headimgurl = hashMap.get("figureurl_qq_2").toString();
                userInfo.sex = "男".equals(hashMap.get("gender").toString()) ? "1" : "2";
                callBack.callBack("登录成功", userInfo);
            }
        }

        @Override
        public void onError(Platform platform, int code, Throwable throwable) {
            if(callBack == null) {
                return;
            }
            if(throwable == null) {
                callBack.callBack("登录异常", null);
                return;
            }

            String messageJson = throwable.getMessage();
            if(messageJson != null) {
                try {
                    JSONObject jsonObject = new JSONObject(messageJson);
                    if(jsonObject.getInt("ret") == 100030 && !platform.isSSODisable()) {
                        doAuthorize(true);
                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                messageJson = "";
            }

            callBack.callBack(String.format("【登录失败】错误代码：%s，详情：%s", code, messageJson), null);
        }

        @Override
        public void onCancel(Platform platform, int code) {
            if(callBack != null) {
                callBack.callBack("取消登录", null);
            }
        }
    };

    private QqPlatform() {}

    @Override
    public void authorize(ThirdPlatformAuthorizeCallBack callBack) {
        this.callBack = callBack;
        doAuthorize(false);
    }

    private void doAuthorize(boolean disable) {

        Platform platform = ShareSDK.getPlatform(QQ.NAME);

        if(!platform.isClientValid()) {
            callBack.callBack("QQ未安装，请先安装QQ", null);
            return;
        }

        if (platform.isAuthValid()) {
            String userId = platform.getDb().getUserId();
            if (userId != null && userId.length() > 0) {
                platform.removeAccount(true);
            }
        }

        platform.setPlatformActionListener(platformActionListener);
        platform.SSOSetting(disable);
        platform.showUser(null);
    }
}
